package cn.wang.web.shiro;

import cn.wang.entity.sysadmin.Module;
import cn.wang.entity.sysadmin.Role;
import cn.wang.entity.sysadmin.User;
import cn.wang.service.sysadmin.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 权限认证
 * Created by john on 2017/11/3.
 */

public class AuthRealm extends AuthorizingRealm {
    private UserService userService;
    private CustomCredentialsMatcher credentialsMatcher;

    /*
    * 授权
    *查询出用户权限模块 使用shiro 标签判断显示
    * */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

        SimpleAuthorizationInfo result = new SimpleAuthorizationInfo();
        //封装用户权限
        List<String> permissionList = new ArrayList();
        User user = (User) principalCollection.fromRealm(this.getName()).iterator().next();
        //将用户数据放入permissionList
        Set<Role> roleSet = user.getRoleSet();
        for (Role role : roleSet) {
            Set<Module> modules = role.getModuleSet();
            for (Module module : modules) {
                permissionList.add(module.getCpermission());
            }
            //将permissionList加入shiro中
            result.addStringPermissions(permissionList);

        }
        return result;
    }

    public CustomCredentialsMatcher getCredentialsMatcher() {
        return credentialsMatcher;
    }

    /*
    * 认证
    *
    * */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("开始认证");
        //AuthenticationToken接口 向下转型
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        //获取名字查询信息
        User user = userService.findByUserName(token.getUsername());

        //为了避免出现弄session 1关闭懒加载 2 加载出需要的数据
        user.getDept().getDeptName();

        System.out.println(user.getDept().getId());
        Set<Role> roleSet = user.getRoleSet();
        for (Role role : roleSet
                ) {
            Set<Module> moduleSet = role.getModuleSet();
            for (Module module : moduleSet
                    ) {
                module.getCpermission();
            }
        }
        //将数据放入info让shiro识别
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassWord(), this.getName());
        return info;
    }

    public void setCredentialsMatcher(CustomCredentialsMatcher credentialsMatcher) {
        this.credentialsMatcher = credentialsMatcher;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
